<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

if(!function_exists('__autoload')){
function __autoload($classname){
    global $CONFIG;
    if(class_exists($classname, false)) return;
    $filename = str_replace('_', '/', $classname).'.php';
    if(file_exists($CONFIG->classesDir.'/'.$filename)){
        require_once($CONFIG->classesDir.'/'.$filename);
        if(class_exists($classname, false)) return;
    }elseif(substr(strtolower($classname), 0, 11) == 'jfx_module_'){
        // try to locate the module
        $moduleKey = str_replace('jfx_module_', '', strtolower($classname));
        if(is_dir($CONFIG->modulesDir.'/'.$moduleKey) && is_readable($CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php')){
            require_once($CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php');
            if(class_exists($classname)) return true;
            else throw new exception('Class not found in file: '.
                    $CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php
                    '.$className);
        }
        
    }elseif(substr(strtolower($classname), 0, 10) == 'jfx_theme_'){
        $themeKey = str_replace('jfx_theme_', '', strtolower($classname));
        if(is_dir($CONFIG->themesDir.'/'.$themeKey) && is_readable($CONFIG->themesDir.'/'.$themeKey.'/'.$themeKey.'.theme.php')){
            require_once($CONFIG->themesDir.'/'.$themeKey.'/'.$themeKey.'.theme.php');
            if(class_exists($classname)) return true;
            else throw new exception('Class not found in file: '.
                    $CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php
                    '.$className);
        }
    }
    return false;
}
}




/**
 * Regenerates keys so the first key is 0 and greatest key is (count($arr)-1),
 *  maintaining natural order of values
 *
 * @param array $array
 * @return array $updatedArray
 */
if(!function_exists('array_regenerate_keys')){
function array_regenerate_keys($array)
{
    $count = 0;
    $arr2 = array();
    foreach($array as $v){
        $arr2[$count] = $v;
        $count++;
    };
    unset($array, $count);
    return $arr2;
}
}



/**
 * Array_Insert function to place a value into an array at a given position
 *
 * @param array $input
 * @param int $position
 * @param mixed $value
 * @return array $updatedInputArray
 */
if(!function_exists('array_insert')){
function array_insert ($input, $position, $value)
{
    // we will definitely get the result back in an array, pos 1
    if (!is_array ($input))
    {
        $array = array ($input);
        $array[$position] = $value;
        return $array;
    }
    // move each key higher to make room for the new key=>value set
    for ($i = count($input); $i > $position; $i--)
    {
        $input[$i] = $input[$i - 1];
    }
    // insert the value to the position
    $input[$position] = $value;

    return $input;
}
}



/**
 * Get the value from $_POST[$key] or $_FILES[$key] or return the alternative value.
 * Truncate to $strlen and sanitize with Antz_Input
 *
 * @param string $key
 * @param mixed $alt
 * @param int $strlen
 * @return mixed $value
 */
if(!function_exists('post')){
function post($key, $alt='', $strlen=255)
{
    $input = JFX::registry('JFX_Input');
    $newval = $input->getPostValue($key, $strlen, false);
    if($newval !== false) return $newval;
    $newval = $input->getFilesValue($key, $strlen, false);
    if($newval !== false) return $newval;
    return $alt;
}
}



/**
 * Get the value from $_GET[$key] or return the alternative value. Truncate to $strlen and sanitize
 * with Antz_Input
 *
 * @param string $key
 * @param mixed $alt
 * @param int $strlen
 * @return mixed $value
 */
if(!function_exists('get')){
function get($key, $alt='', $strlen=5000)
{
    return (array_key_exists($key, $_GET))
        ? substr($_GET[$key], 0, $strlen)
        : $alt
        ;

}
}



/**
 * Delete all files and folders recursively
 * @param string $dirname
 */
if(!function_exists('unlink_directory')){
function unlink_directory($dirname){
    if(!is_dir($dirname)){
        if(file_exists($dirname)) unlink($dirname);
        return;
    }
    $dh = opendir($dirname);
    while($file = readdir($dh)){
        if($file == '.' || $file == '..') continue;
        if(is_dir($dirname.'/'.$file)) unlink_directory($dirname.'/'.$file);
        else unlink($dirname.'/'.$file);
    }
    rmdir($dirname);
}
}



/**
 * Copy a directory recursively
 * @param string $dirnameSource
 * @param string $dirnameDest
 */
if(!function_exists('copy_directory')){
function copy_directory($dirnameSource, $dirnameDest){
    if(!is_dir($dirnameDest)){
        mkdir($dirnameDest);
    }
    $dh = opendir($dirnameSource);
    while($file = readdir($dh)){
        if($file == '.' || $file == '..') continue;
        if(is_dir($dirnameSource.'/'.$file)) copy_directory($dirnameSource.'/'.$file, $dirnameDest.'/'.$file);
        else copy($dirnameSource.'/'.$file, $dirnameDest.'/'.$file);
    }
}
}










